package MultiNetworkingBase;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;

//TODO: Each unit should have a class (which holds its max hp in addition to other things)

public class WarTug {
	public Screen currentScreen = Screen.inGame; //this is the mode of the game,determines which game loop to run
	private GraphicsManager gManager;
	private InputHandler iHandler;
	public UnitManager uManager;
	private Network network;
	
	public static String name; //Name of the player
	
	private String host = "127.0.0.1"; //TODO: THIS SHOULD BE SET IN THE CODE, IP address of the server
	
	
	private void setup() {  //Called as soon as the application starts.
		gManager = new GraphicsManager();
		uManager = new UnitManager(gManager);
		iHandler = new InputHandler();
		network = new Network(uManager);
		
		
		//TODO: Each screen type should have its own .java file
		//TODO: THIS REALLY ONLY SHOULD BE DONE ONCE WE DECIDE TO START A GAME (Go through menu first..)
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Your Name?");
	    try {
	    	name = br.readLine();
	    } catch (IOException e) {
	    	System.out.println("Error!");
	    	System.exit(1);
	    }
		System.out.println("Create Server? (y/n)");
	    String makeServer = null;
	    try {
	    	makeServer = br.readLine();
	    } catch (IOException e) {
	    	System.out.println("Error!");
	    	System.exit(1);
	    }
	    if (makeServer.equalsIgnoreCase("y")){
	    	network.setUpClientAndServer(true,host);
	    } else {
	    	network.setUpClientAndServer(false,host);
	    }
	    
	    GameScreen.switchToMainMenu(gManager, uManager, iHandler, this);
	}
	
	
	//TODO:WHERE TO PUT THIS??
	public static long getTime() {
	    return (Sys.getTime() * 1000) / Sys.getTimerResolution();
	}
	
	
	private void start() {
		//This is the main game loop for the client
		long start = System.currentTimeMillis();
		long delta;
		
		// Setup the main menu screen
		
		

		while (!Display.isCloseRequested()) {
			Display.sync(100); //This limits the FPS to 100
			gManager.updateFPS();
			
			delta = System.currentTimeMillis() - start;
			start = System.currentTimeMillis();
			
			switch (currentScreen) {
				case mainMenu:
					runGameLoop(delta);
					break;
				case gameCreateMenu:
					break;
				case gameJoinMenu:
					runGameLoop(delta);
					break;
				case inGame:
					runGameLoop(delta);
					break;
				case postGameStats:
					break;
				default:
					System.err.println("Error on screen index!!!!");
					System.exit(-1);
					//This shouldn't ever happen
			}
			
		}
		
		//Clean up and end
		network.endNetworkConnection(); //TODO: move to the end game logic
		gManager.destroySelf();
	}
	
	//TODO: Move to own .java file
	private void runGameLoop(long delta) {
		//Main ingame loop
		iHandler.handleMouse();
		iHandler.handleKeyboard(this);
		uManager.updateUnits(delta);
		gManager.renderAll();
	}
	
	public Screen getCurrentScreen(){
		return currentScreen;
	}
	
	public void transitionScreens(Screen from, Screen to){
		gManager.clearDrawableEntities();
		
	}
	
	
	
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.setProperty("org.lwjgl.librarypath",System.getProperty("user.dir") + "/libs/lwjgl-2.8.3/native"); //set natives directory
		WarTug game = new WarTug();
		game.setup(); //Here do all the local pre-game setup
		game.start(); //This is the main loop that will run all client side game logic

	}

}
